package com.nononsenseapps.feeder.db.room

import androidx.room.*
import com.nononsenseapps.feeder.db.entity.FeedSyncItem

@Dao
interface FeedSyncItemDao {
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insert(item: FeedSyncItem): Long

    @Update
    fun update(item: FeedSyncItem): Int

    @Delete
    fun delete(item: FeedSyncItem)
}

/**
 * Inserts or updates feed depending on if ID is valid. Returns ID.
 */
fun FeedSyncItemDao.upsert(feedSyncItem: FeedSyncItem): Long = when (feedSyncItem.id > ID_UNSET) {
    true -> {
        update(feedSyncItem)
        feedSyncItem.id
    }
    false -> {
        val id=insert(feedSyncItem)
        feedSyncItem.id=id
        id
    }
}